package com.caigou.modules.data.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.bean.CommentBean
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.data.bean.DataDetailBean
import com.caigou.modules.data.bean.LawDetailBean
import com.caigou.modules.data.bean.LawItemBean
import com.caigou.modules.data.repository.DataRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/29
 */
@HiltViewModel
class LawViewModel @Inject constructor(private val repository: DataRepository) : ViewModel() {

    val lawListLiveData = MutableLiveData<Resource<MutableList<LawItemBean>>>()
    val lawDetailLiveData = MutableLiveData<Resource<LawDetailBean>>()
    val commentListLiveData = MutableLiveData<Resource<CommentBean>>()

    fun getLawList() {
        lawListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getLawList()
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<LawItemBean>>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    lawListLiveData.value = it
                }
        }
    }

    fun getLawDetail(formulaId: Int, userId: String) {
        lawDetailLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getLawDetail(formulaId, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<DataDetailBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    lawDetailLiveData.value = it
                }
        }
    }

    fun getCommentList(page: Int, limit: Int, formulaId: Int, sort: Int, userId: String) {
        commentListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getCommentList(page, limit, formulaId, 1, sort, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<CommentBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    commentListLiveData.value = it
                }
        }
    }
}